React = require('react')
Card = require('antd-mobile/lib/card')
# ListView = require('antd-mobile/lib/list-view')
Flex = require('antd-mobile/lib/flex')
Icon = require('antd-mobile/lib/icon')
Button = require('antd-mobile/lib/button')
Toast = require('antd-mobile/lib/toast')
RefreshControl = require('antd-mobile/lib/refresh-control')
TopNotice = require('antd-mobile/lib/notice-bar')
{StyleSheet,DatePickerAndroid,TextInput,View,Image,Text,ScrollView,TouchableHighlight,StyleSheet,Navigator,ListView,ActivityIndicator,TouchableOpacity} = require('react-native')
Dimensions = require('Dimensions')
{ width, height } = Dimensions.get('window')
{View} = require('react-native')
{fetchGxInfo,PostGxsq,resetState} = require('../actions/page_gxlxsq.js')
{connect} = require('react-redux')
_ = require('underscore')
moment = require('moment')
backImg = require('../img/back.png')

Page_Gxlxsq = React.createClass
  getInitialState:->
    @props.fetchInfo(@props.userInfo.pilotCode)
    {
      startDate:''
      endDate:''
    }
  _processTime:(time)->
    t = moment(time,'YYYY-M-D')
    t.format('YYYY-MM-DD')
  startDateHandler:->
    DatePickerAndroid.open().then (result)=>
      debugger
      if result.action isnt DatePickerAndroid.dismissedAction
        str = @_processTime "#{result.year}-#{result.month+1}-#{result.day}"
        @setState startDate: str

  endDateHandler:->
      DatePickerAndroid.open().then (result)=>
        if result.action isnt DatePickerAndroid.dismissedAction
          @setState endDate: @_processTime "#{result.year}-#{result.month+1}-#{result.day}"
  addClickHandle:->
    obj = {}
    debugger
    obj.CHPILOTCODE=@props.userInfo.pilotCode
    obj.CHPILOTNAME=@props.userInfo.pilotName
    obj.startTime=@state.startDate
    obj.endTime=@state.endDate
    obj.SQLB='L'
    obj.QJSY=''
    @props.postData obj
  componentDidUpdate:->
    if @props.success
      Toast.success "保存成功",3,=>
        @props.reset()
    else if @props.error
      Toast.fail @props.error,3,=>
        @props.reset()

  render:->
    <View style={width:width,height:height,flexDirection:'column'}>
      <View style={{flexDirection:'row',flex:0,height:60,alignItems:'center', justifyContent:'center',backgroundColor:'#0066FF'}}>
        <View style={{flex:1,padding:10}}>
          <TouchableOpacity onPress={=>@props.navigator.pop()}>
            <Image source={backImg}  style={{height:30,width:30,resizeMode:'contain'}}/>
          </TouchableOpacity>
        </View>
        <View style={{flex:3,justifyContent:'center',alignItems:'center'}}>
          <Text style={{fontSize:20,color:'#ffffff'}}>公休/轮休申请</Text>
        </View>
        <View style={{flex:1,alignItems:'flex-end',padding:10}}>
        </View>
      </View>
      <ScrollView style={{backgroundColor:'#fffff0',paddingTop:10,height:height-80}}>
        <View style={{flexDirection:'column',marginBottom:10}}>
          <View style={styles.row}>
            <View style={styles.cell}>
              <Text style={styles.text}>引航员</Text>
              <Text style={styles.text}>{@props.userInfo.pilotName}</Text>
            </View>
            <View style={styles.cell}>
              <Text style={styles.text}>已轮休天数</Text>
              <Text style={styles.text}>{@props.YLXTS}</Text>
            </View>
          </View>
          <View style={styles.row}>
            <View style={styles.cell}>
              <Text style={styles.text}>剩余轮休天数</Text>
              <Text style={styles.text}>{@props.SYTS}</Text>
            </View>
            <View style={styles.cell}>
              <Text style={styles.text}>已公休天数</Text>
              <Text style={styles.text}></Text>
            </View>
          </View>
          <View style={styles.row}>
            <View style={styles.cell}>
              <Text style={styles.text}>剩余公休天数</Text>
              <Text style={styles.text}></Text>
            </View>
            <View style={styles.cell}>
              <Text style={styles.text}>公休/轮休</Text>
            </View>
          </View>
          <View style={styles.row}>
            <Text>申请日期</Text>
          </View>
          <View style={styles.row}>
            <View style={{flex:1}}>
              <TextInput style={{fontSize:18}} value={@state.startDate} onFocus={@startDateHandler}></TextInput>
            </View>
            <View>
              <Text style={{fontSize:18}}>~</Text>
            </View>
            <View style={{flex:1}}>
              <TextInput style={{fontSize:18}} value={@state.endDate} onFocus={@endDateHandler}></TextInput>
            </View>
          </View>
          <View style={styles.row}>
            <View style={styles.cell}>
              <Text style={styles.text}>申请事由</Text>
              <Text style={styles.text}></Text>
            </View>
          </View>
          <View style={styles.row}>
            <Button type='primary' onClick={@addClickHandle}>提交申请</Button>
          </View>
        </View>
      </ScrollView>
    </View>


styles = StyleSheet.create
  row:
    flexDirection:'row'
    borderWidth:0.3
    borderStyle:'solid'
    borderColor:'#CCCCCC'
    backgroundColor:'#ffffff'
    padding:10
    marginLeft:10
    marginRight:10
  cell:
    flex:1
    flexDirection:'row'
  text:
    flex:1

mapStateToProps = (state)->
    YLXTS:state.page_gxlxsq.YLXTS
    SYTS:state.page_gxlxsq.SYTS
    posting:state.page_gxlxsq.posting
    success:state.page_gxlxsq.success
    error:state.page_gxlxsq.error
    userInfo:state.userInfo
    connected:state.appState.connected
mapDispatchToProps = (dispatch,wonProps)->
  fetchInfo:(pilotCode)->
    dispatch fetchGxInfo(pilotCode)
  postData:(obj)->
    dispatch PostGxsq(obj)
  reset:->
    dispatch resetState()

module.exports = connect(mapStateToProps,mapDispatchToProps)(Page_Gxlxsq)
